package com.ruoyi.record.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.record.common.RecordConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.WordStudyRecordMapper;
import com.ruoyi.record.domain.WordStudyRecord;
import com.ruoyi.record.service.IWordStudyRecordService;

/**
 * 单词学习记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-04
 */
@Service
public class WordStudyRecordServiceImpl implements IWordStudyRecordService
{
    @Autowired
    private WordStudyRecordMapper wordStudyRecordMapper;

    @Autowired
    private WordRecordCommonService wordRecordCommonService;

    /**
     * 查询单词学习记录
     *
     * @param id 单词学习记录主键
     * @return 单词学习记录
     */
    @Override
    public WordStudyRecord selectWordStudyRecordById(Integer id)
    {
        return wordStudyRecordMapper.selectWordStudyRecordById(id);
    }

    /**
     * 查询单词学习记录列表
     *
     * @param wordStudyRecord 单词学习记录
     * @return 单词学习记录
     */
    @Override
    public List<WordStudyRecord> selectWordStudyRecordList(WordStudyRecord wordStudyRecord)
    {
        return wordStudyRecordMapper.selectWordStudyRecordList(wordStudyRecord);
    }

    @Override
    public List<WordStudyRecord> selectWordStudyRecordListByUser(WordStudyRecord wordStudyRecord)
    {
        return wordStudyRecordMapper.selectWordStudyRecordListByUser(wordStudyRecord);
    }

    @Override
    public List<WordStudyRecord> selectWordStudyRecordWriteList(WordStudyRecord wordStudyRecord)
    {
        return wordStudyRecordMapper.selectWordStudyRecordWriteList(wordStudyRecord);
    }

    /**
     * 新增单词学习记录
     *
     * @param wordStudyRecord 单词学习记录
     * @return 结果
     */
    @Override
    public int insertWordStudyRecord(WordStudyRecord wordStudyRecord)
    {
        WordStudyRecord record = wordRecordCommonService.insertList(wordStudyRecord);
        record.setCreateTime(DateUtils.getNowDate());

        int inserted = wordStudyRecordMapper.insertWordStudyRecord(record);

        //处理是否有未完成任务
        JSONObject jsonObject = new JSONObject();
        if (record.getComplete() == null || record.getComplete().isEmpty() || record.getComplete().isBlank()) {
            jsonObject.put("book", record.getBook());
            jsonObject.put("unit", record.getUnit());
        }
        else {
            jsonObject.put("group", record.getComplete()); //组号
        }
        jsonObject.put("rate",record.getCorrectRate());
        jsonObject.put("type", RecordConstant.TASK_TYPE_WORD_STUDY);
        Long userId = record.getUserId();
        wordRecordCommonService.handleProcess(jsonObject, userId);

        return inserted;
    }

    /**
     * 修改单词学习记录
     *
     * @param wordStudyRecord 单词学习记录
     * @return 结果
     */
    @Override
    public int updateWordStudyRecord(WordStudyRecord wordStudyRecord)
    {
        wordStudyRecord.setUpdateTime(DateUtils.getNowDate());
        return wordStudyRecordMapper.updateWordStudyRecord(wordStudyRecord);
    }

    /**
     * 批量删除单词学习记录
     *
     * @param ids 需要删除的单词学习记录主键
     * @return 结果
     */
    @Override
    public int deleteWordStudyRecordByIds(Integer[] ids)
    {
        return wordStudyRecordMapper.deleteWordStudyRecordByIds(ids);
    }

    /**
     * 删除单词学习记录信息
     *
     * @param id 单词学习记录主键
     * @return 结果
     */
    @Override
    public int deleteWordStudyRecordById(Integer id)
    {
        return wordStudyRecordMapper.deleteWordStudyRecordById(id);
    }

    @Override
    public List<WordStudyRecord> selectWordWriteRecordListByUser(WordStudyRecord record) {
        return wordStudyRecordMapper.selectWordWriteRecordListByUser(record);
    }
}
